<template>
<div>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px" size="small">
    <el-form-item label="区域" prop="buildingId">
       <el-input placeholder="请选择 区域"
            style="width:100%"
            class="filter-item"
            @click.native="inputBuildName()"
            v-model="dataForm.buildName">
        </el-input>
      <!-- <el-select v-model="dataForm.meterCode" filterable>
        <el-option v-for="item in meterInfoList" :key="item.meterCode" :label="item.meterCode+'['+item.meterAddr+']'" :value="item.meterCode" />
      </el-select> -->
    </el-form-item>
      <el-form-item label="归属类型" prop="ascriptionType">
        <el-radio v-model="dataForm.ascriptionType" label="0" @change="changeCustType">租户</el-radio>
        <el-radio v-model="dataForm.ascriptionType" label="1"  @change="changeCustType">部门</el-radio>
      </el-form-item>
    <el-form-item label="归属租户号" v-if="dataForm.ascriptionType==='0'" >
      <el-select v-model="dataForm.subscriberId" filterable clearable style="width:100%;">
        <el-option v-for="item in subscriberInfoList" :key="item.subscriberId" :label="item.subscriberName" :value="item.subscriberId" />
      </el-select>
    </el-form-item>
    <el-form-item label="归属部门" v-if="dataForm.ascriptionType==='1'">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <div style="height: 250px">
            <el-scrollbar style="height: 100%;width: 100%">
              <el-tree
                :data="orgInfoList"
                :props="orgListTreeProps"
                node-key="organizationId"
                ref="menuListTree"
                @current-change="menuListTreeCurrentChangeHandle"
                :default-expand-all="true"
                :highlight-current="true"
                :expand-on-click-node="false">
              </el-tree>
            </el-scrollbar>
          </div>
        </el-popover>
        <el-input v-model="dataForm.organizationName" v-popover:menuListPopover :readonly="true" placeholder="点击选择归属部门" class="menu-list__input"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="small">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" size="small">确定</el-button>
    </span>
  </el-dialog>
  <el-dialog title="请选择区域" :visible.sync="showBuildTree">
    <el-scrollbar style="height: 100%;width: 100%">
        <el-tree
          :style="'height:'+containerHeight+'px'"
          ref="buildTree"
          highlight-current
          node-key="id"
          :data="buildingInfoList"
          :expand-on-click-node="false"
          :props="menuListTreeProps"
          @node-click="selectMeterBuild"
          default-expand-all
        >
          <span class="custom-tree-node"  slot-scope="{ node, data }">
            <span :dataType="data.type">
                <!-- <i class="el-icon-setting"  v-if="data.type=='b'"></i> -->
                <i class="el-icon-star-off"  v-if="data.type=='d'"></i>
                {{ node.label }}
            </span>
          </span>
        </el-tree>
    </el-scrollbar>
    <div slot="footer" class="dialog-footer">
      <el-button @click="showBuildTree = false">取 消</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
  import * as Util from "@/utils";
  export default {
    data () {
      return {
        showBuildTree:false,
        treeMeterDataList:[],
        menuListTreeProps: {
          label: "buildingName",
          children: "children"
        },
        orgListTreeProps: {
          label: 'organizationName',
          children: 'children'
        },
        buildingInfoList: [],
        subscriberInfoList:[],
        orgInfoList:[],
        visible: false,
        dataForm: {
          id: 0,
          buildingId: '',
          buildName:'',
          subscriberId: '',
          organizationId: '',
          organizationName:'',
          ascriptionType:'1'
        },
        dataRule: {
          buildingId: [
            { required: true, message: '区域不能为空', trigger: 'change' }
          ],
          subscriberId: [
            { required: true, message: '归属租户号不能为空', trigger: 'blur' }
          ],
          organizationId: [
            { required: true, message: '归属部门不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      containerHeight() {
        return Util.getClientHeight() - 243;
      },
    },
    created(){
      // this.getbuildTreeList();
      // this.getOrgDataList()
    },
    methods: {
      changeCustType(){

        if(this.dataForm.ascriptionType ==='0'){

          this.dataForm.organizationId =''

        }else{

          this.dataForm.subscriberId =''
        }

      },
      menuListTreeCurrentChangeHandle(data){
        this.dataForm.organizationId = data.organizationId;
        this.dataForm.organizationName = data.organizationName;
      },
      init (id,buildingInfoList, subscriberInfoList,orgInfoList) {
        this.subscriberInfoList = subscriberInfoList
        this.dataForm.buildName =''
        this.dataForm.organizationName=''
        this.dataForm.subscriberId=''
        this.dataForm.organizationId=''
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecsbuildingascription/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.buildingId = data.ecsbuildingascription.buildingId
                this.dataForm.subscriberId = data.ecsbuildingascription.subscriberId
                this.dataForm.organizationId = data.ecsbuildingascription.organizationId
              this.dataForm.ascriptionType = data.ecsbuildingascription.ascriptionType+''
              this.getbuildTreeList(this.dataForm.buildingId );
              this.getOrgDataList(this.dataForm.organizationId);
              }
            })
          }else{
            this.getbuildTreeList( );
            this.getOrgDataList();
          }
        })
      },
      //获取全量部门
      getOrgDataList (orgId) {
        this.$api.okhttp({
          url: this.$http.adornUrl('/ecs/ecsorganizationinfo/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10000000,
            "customerId": this.$util.cookies.get('customer_id'),
          })
        }).then((data) => {
          if (data && data.code === 0) {
          this.orgInfoList =Util.treeDataTranslate(data.page,'organizationId','parentOrgId');
          if(orgId){
            for(let i=0;i<data.page.length;i++){
              if(data.page[i].organizationId===orgId){
                this.dataForm.organizationName =data.page[i].organizationName;
              }
            }
          }
        } else {
          this.orgInfoList = []
        }
      })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if(this.dataForm.subscriberId===''&&this.dataForm.organizationId===''){
              this.$message.warning("请选择租户或部门")
              return ;
            }
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecsbuildingascription/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'buildingId': this.dataForm.buildingId,
                'ascriptionType': this.dataForm.ascriptionType,
                'subscriberId': this.dataForm.subscriberId,
                'organizationId': this.dataForm.organizationId,
                "customerId": this.$util.cookies.get('customer_id'),
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      inputBuildName(){
        this.showBuildTree = true;
      },
      selectMeterBuild(data){
        if(data.buildingId!==-1){
          this.dataForm.buildingId = data.buildingId;
          this.dataForm.buildName = data.buildingName;
          this.showBuildTree = false;
        }
      },
      getbuildTreeList(buildingId){
        this.$api.okhttp({
          url: this.$http.adornUrl('/ecs/ecsbuildinginfo/select'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10000000,
            'projectid': this.$util.cookies.get("cuuren_projectId"),
            "customerId": this.$util.cookies.get('customer_id'),
          })
        }).then((data) => {
          if (data && data.code === 0) {
          this.buildingInfoList= Util.treeDataTranslate( data.menuList,'buildingId','parentBuildingId');
          if(buildingId){
            for(let i=0;i<data.menuList.length;i++){
              if(data.menuList[i].buildingId===buildingId){
                this.dataForm.buildName =data.menuList[i].buildingName;
              }
            }
          }
        } else {
          this.buildingInfoList = []
        }
      })
      },
    }
  }
</script>
<style>
.el-scrollbar__thumb{
  right: 0px;
}
.el-scrollbar__wrap{
  margin-right: 0px;
}
</style>
